#include "test.h"
#include "speed.h"

void byte_fm_str(u8* b, const char* s) {
    static const u8 table[] = {
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
            0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    u64 slen = strlen(s);
    for (int i = 0; i < slen; i += 2)
        b[i >> 1] = table[s[i]] << 4 | table[s[i + 1]];
}

void XMen_Test_Sign(const char* userid, const char* private_key, const char* public_key, const char* input_data) {
    u8 data[strlen(input_data) >> 1];
    sm2_private_key prikey;
    sm2_signature signature;

    byte_fm_str(data, input_data);
    byt_fm_str(prikey.prikey,   private_key);
    byt_fm_str(prikey.pubkey.x, public_key);
    byt_fm_str(prikey.pubkey.y, public_key + 64);


    u8 dgst[32], buf[72], *_buf = buf;
    u64 buf_len = 0;

    sm2_sign_init(dgst, &prikey.pubkey, userid, strlen(userid), data, sizeof(data));
    sm2_sign(&signature, &prikey, dgst);
    sm2_sig_to_der(signature, &_buf, &buf_len);
    printf("\n*************************************************\n");
    printf("Digst:\n");
    for (int i = 0; i < 32; i++) {
        printf("%02x", dgst[i]);
    }
    printf("\n");
    printf("Signature(ASN.1):\n");
    for (int i = 0; i < buf_len; i++) {
        printf("%02x", buf[i]);
    }

    printf("\n");
    printf("Signature R:\n");
    for (int i = 0; i < 32; i++) {
        printf("%02x", signature.r[i]);
    }
    printf("\n");
    printf("Signature S:\n");
    for (int i = 0; i < 32; i++) {
        printf("%02x", signature.s[i]);
    }
}

int main() {
   test_all();
   speed_all();
   char userid[] = "1234567812345678";
   char private_key1[] = "BF7224726BCF102DF855915246B113655B3976F4D2A4D8497A14ADD50D82F29C";
   char private_key2[] = "2E2766405F264E74FE704564130C0E455CF12B422B1DC774F57D7AD75E1F4E86";
   char private_key3[] = "81515EDBF5D2783BDD81B684BBE2FF027A5CBAE7370C52CC65E53033E85E837B";
   char public_key1[]  = "50E278ABBAB45CA2E8134199E0EDE0CAA694D3DE26C9FCEFFCDFEEDC8592CC9D"
                         "4E6EDE362A2C6247A156336384DC9C09E36B14B5639BEBDD1B99C92A4E4644CD";
   char public_key2[]  = "152E1668546B40A921C94DCA04F3A556BFFE8120A4CD5A1D914C83DAB37242D1"
                         "60C9DAB3797241D1E29E280D577971FBB0FCF424C8B37534DC3551CA68EF2957";
   char public_key3[]  = "BA0BE48FE597AAD90BB14125809FA562A9FE5105ED585624F5EBEBD0CF68F7FA"
                         "0121FDD4EC2CA3051DB46685E973068F72A57CD2ED15BD670F437CC8CAB48C8C";
   char input_data1[]  = "7C93AA2CB289D3639453CE12408F81295333528D0481163C76C92C2ED5A28F4846E63BC46A17AD1F894D48F71ADD661C00529B961C6D86E7B4B0E60246F0D814100762ACA15F89C78126C54AC7CBF742391B65C70C9D827EB99C33AE14F6D2660A80D3CDB372D142D4C9FD158D7722F81C7441213AF3159C53417D10692FD6043A792CA66CBD014AE0CEC76918F774F4EBA750D68F88D5413D54AE08C9B1A8F7D7A61B9C59E163A14D9BF99F472013C2E6E2E76FD885B682B135D90F2F4945E99489AA807FDD8D24745B2623C253BF55C3DE6A91A612A5979090EF7D804D0451EA5A2B1F23A3E97204246FB3FEFECBF1BCEFA42F447DA263289C84856A3ED5DCBEBBEA1C9FF986C0B44508EB8B04309F7228A8BE63D3212DAA17C7E8E2EE7644C5F31B7ACD7AF4E98C03BAB754B7CC742641BBA73E397D3B828B1CA20C749727BD3BDC45ACEEC18B82D476AA27ED36A61295AD914E0A69ED24726FDF2DF63A1D7EA8997558CA9DD9C44E15F1F40B5061A29FA7E43BD87F47069D420675C84B8CA5037C20BA1759CC3F8224392B3BF586BB429A38830CB8191550312ADBBF12B18F537D3FC5F40EF6F4D72A2F4245AE3F30F2654EB4C5C68F527612D976D901B7BD8AEB5E8E9071B91C22A3039531A3BCE0FF1D8DF55542AF9D2D147552E5771C3879E4407B0E86FD3012BDFA725E3BDD4233F8EA2A914D0006DC93D832DE11C7060EB2D9F90CAE502672EF8EAC2A31BD3B3594230CAA92DB6D0066FFA6C2A8E20EF56E2695EE5BC017D18A4F00A68F4C260BD672E2C5B8DF4CBB70C64A5E03032E50AB9E997A0F1FB0C498328EA6E7856A3FFBBA5F467745DBE1460EA785C61B4B07FE39E661A5B8F62C145E534EFF7D06CC343A6D49DF0CB2057D41897C024A1FBCA4857553ED498DEACF1923F0A2A8155FAB0AC5CB2C7837170E0871DF4895C1D0032DF7881F0C5F2BD493223F73BA21465FE34344FF705C88F5A7CE9D997E21CD365D21BB59F5B6B8A89C49EE7A8C68A93F43CDC69CF7A9BC61EB3225648E5B4D90AB136D8259D3B0606FC0B48F6ACC0FE232323F21D137E8C906BE5FDCC73401C2234CC4955ED9EA3D1906605DAC788EFF8A8649E8CE0B887E58164DB6FF5C23A95177E62D504D6C158A3F39F40587A317FDD63E1DC698222CA1CF5901B9F8BFE2E5B373DD2EBCCCA7FD8C1D58B9DF5B7DBEC250098144DCC759563FCA64674D75A6524F6F7E8DAD5870953C6DD11E0FD73680C837908FF07A95D891C151D8F4F5C84C26ECD672D2E5B390E335CBF25855C23B15BC3448BD916EF21528A457C3795F985AF1867685096504C87B60E7574E04A6E0E546588F471E3B77B2D4690030298D67FDEA7C52CE2FEC54AEFB4FFB5981AB44CCD196C2E0192053FC6A01EB372E61FBA5EAE26A4F2E32D295DADF189FEDD14A1F03C568B3D12C09E927";
   char input_data2[]  = "BFF0EABBB6FDF1ABBC591E87FD4E32C6149A84F4241FFAF3A72AEB719A84CFE859BB1C3E0E7653734DADAA7F55D797ABB01A87C886842E12AD7F577CB3AC827144BED67435AB75CBF8AE7D5B05D7ED39A932D5BE08A42AEBA90BE43A9A87890607301103D3C23C79042E9E0E7CC945038CFAF9D9F97A0FF90705A236B10AE52EEEBCAA0718AFDBAFB19E500225071C920F9F5B79AF7961B03F4E02B1C6B7021029FEC35F8A71F6B0D79A5047FFF9ECF31F5FAB469DD9F11992FB7C1BF703C2B48C004DF9DF9147702A0A67C457B59EBDA8E29716E90A8075A47B8D3CC323A5EBFB8F3A430574F9E1935F20B1DE074018D081EB6D2BBE309E215C4D5CDF78C531C304E0FAC760015B7DA5873ABB6CE7FA26001EF39053AE0F01889699C47C66A3C8D528C1852C4F4D536012AAA229E2A3BEE9FE0ACE74692472F21C653769013B8511B6D7A16BCB70FF3FFD901582D867EF0DEB038349B1513C204BADE7335E697ACB631B2909FD0013DC5E6571313B399F5EEAAA21609356E9652E3D46BA4DB893A4B4FD3849799360706EE0C086EBF77CEC8FAE8CAE2FFB59E73B4881B1E3F601F181256A3C9A8ECB4D51980CAE10AB319E03240696699BC4E3D826ACBD76741369299DA04B8C20A57526C87399C0F1C8C5F35324B2BF9ACA9EC3D9ED673EDD6BEC2D8BC3CE421E836FA4FF707FDC2ED5AE58C74826219326C2589860FB5E542C8FA5F3C827FD28A2503BCDE0E1E89D702CA99EE979EE8D0EB3EC8ACBC3CAB52937E5A82C3AB1B94BABC710259C1A51FEF6FD8F38125331930F967E093F52205A5D7C9304A331A2C37982C7661A8D6EB97F0EC12324507100B2B67B64408D5CF3278E4AE23153413FB2C82FA01D14DD3EC45A2D9A9ED866DBBE372476A143CCC754AB6F046ADE76656E395E24FA96431C5BD10D7D44D236F5111AB3E00F496DD0452D8EF8C7C2740C9354215DF243861898869CE7CC3B845CB6CA92E45B9448A12A38C33D3F3087FA240A5A3734FEF783A12F4B42D0CA8781338E61F0F96D00C7801E3DD972CCFBED76B9E06FE1AF60FE191CE94BA6638CC82F993F850E40F583CDA1AFDF27A4F2EC2A4F518437EC663A8ED1D776585BD691B18724DF7ECB3510C748B8D6226641124F14B590B9D95308009F6ECE34F3DC8C826DFA205A49BE27B503E061144C810111C563031D2A9C4893BD82FFFEFA5EB49C7EFA136E4B4204D38C16381DDCF29A3AC591A3998B6448887BD3194C06AEC92D36608C23E41E0EEC537EE1FD83702614FC5FFDCD15A78FF617F0A95C54181C4FA4B909786E33C011F7FD2B09424427BF4B016643AF87CB69792CDEA8A04BF7D0D5A7E7042C8C061AB826E4B85F65C73C6CF667CD52097905596F2F9558F1D6353C77AC3CBC4210A56FEC70BA5D5F56EE7DDAC3BCF96DB4BB5920A78694F73A7E052D00E336B4";
   char input_data3[]  = "DAD89E5BD3FB3E38E7A6D772881DB65F210CBAE224A23D28641EC26468F2195F6714D69D954E0C231B49393DEC7306C871C720C47DADA7A36ACD4EF5A7ED6764F16D11F58B778D5CA8134F23EF387AF0585766079F764ACF8A163AF3A59BDAE9667819BF5F84FC7A832B3C9E2FBC32B89D1D975B894F82A7C37DA8C6BB175C2DD4C40B78E0248EDF8FA9AFE926C24D0D9170C88818DEBAA4F904D5DFCE69D595F8B67E08981F13128C6699C9E4501F9CF535CE9479A0032BC8C3CF5A1816E72C3756196622284B511373AF7EDC283D8F1D50482DC4F24BC8FE0B96423FDC1384AD7AA930C5A07827F87F77349F6C3477DAD05DDEE254DFC7A074B4A2D0949678CAA5CE403B1EDEED070CC29E886BB3D0B5DD8F201F2ADDFDA88B096EAD065B6F8C8C54DC17934FA3A66306FE0AC147517DDD6E2BD832717B6BD7F0985D64BD0498B3C3F857F98323E0B99E9A146C0E95162083CFAB3999945866580573786A3CE401F295C0A6B1218374C534AA484B1484745E016229A1D8BC6B46F1B6B3E3BE36AA151F7853973C2857D2D18D40124905F2B16D92742C56D6FC288209A060F53D183A5F4D82883561AFE7E34CAD33F4DED65664BA1DCEAD61D9A11FDA4A37BC32529E6C5F6435AF825AEC4FF3A52659F3FF3BB17785671598D22ECB09EC3D6AA779F860A08D5BF9665356F36FEFC9DEC95A858D1D9571D5E908DAEB1D7A93AC5880A9E1328811F918903B61BC87CEF53C413256ED87AD98CA8039DB5A68B3BA794305DD80AE24E60A6A4FE2A5676E9430F25C8985D0FB07273144CB641FCC4E611749A7EDD261221AB108573F057F84AD2037725707E73BA27F06D5984BE209C4833FB6BC5F1F80E240A84E5C96DB1B9E6EAD477DD92791E3CC18BBF250517665EA7523B98AC1CA0C47EC6C69AC8E1366770B6740B48CABB6B72F4EAF597AB56F77D5DC934FF0F6DA8E404621879006D7F8D652389AE5940E4536969491E84D3CF67D86533E4262567FA78B830928414E2142E9C204D9D573F4190B25A26E09BB3DBC2536900C65EFFDDB055EFF4C86EA1261235F0B89645D2BD6BE0B0A3EE55F46A10BA282A29A12DEF370A0D8A5D672C7548AECC944359B91EC6E485B39AC6A723BC7D26CBCC86B702C20AA771CF7EACAB9C404745FA4AF4A128726176EB00054E384A6B044BA8F15961EA1CE0B28CE37CAEF83D6AE7D8F16974DF59BEC355090CB5EDF133CDF8C512064D77DAF7AEE0690BE53CAF7DB2309AB17490FA0CDDCFE983C0F438A58072AE8B399D8041A99ACBC7AE2EDC49DC0C0011DCA667FE8C61CF07859F76131EBDFEBDE41C5234AA508EABD291FFB4AEDFE3D09AAF10707B3A4281C03CD1EC15003FED0CB8788457000779FABCA8C4D5F28F6EF1C9CA1A1AD600C779A9292365A19D096592E620CF4A25C0AB79F7B10BEFB1C8B998EC38E";
   XMen_Test_Sign(userid, private_key1, public_key1, input_data1);
   XMen_Test_Sign(userid, private_key2, public_key2, input_data2);
   XMen_Test_Sign(userid, private_key3, public_key3, input_data3);
   test_sm2_enc();

    return 1;

}





